#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<unistd.h>
#include<stdlib.h>
#include<errno.h>
#include<arpa/inet.h>
#include<netinet/in.h>
#include<string.h>


#define ERR_EXIT(m) \
    do { \
        perror(m); \
        exit(EXIT_FAILURE); \
    } while (0)




int main(void)
{
    int sock;
    if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
        //  listenfd = socket(AF_INET, SOCK_STREAM, 0)
        ERR_EXIT("socket error");


    struct sockaddr_in servaddr;
    memset(&servaddr, 0, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(5188);
    servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
    /* inet_aton("127.0.0.1", &servaddr.sin_addr); */

    if (connect(sock, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
        ERR_EXIT("connect error");

    int total = 0;
    int n;
    while(1) {
#if 0
        char recvbuf[1024] = {0};
        memset(recvbuf, 0, sizeof(recvbuf));
        int n = read(sock, recvbuf, sizeof(recvbuf));
        if(n <= 0) {
            ERR_EXIT("read error");
        }

        printf("%s\n", recvbuf);
#endif
        n = write(sock, "A", 1);
        total ++;
        
        printf("n=%d, total=%d.\n", n, total);
        if(n <= 0) {
            break;
        }
    }

    close(sock);


    return 0;
}
